<?php
// +----------------------------------------------------------------------
// | QSCMS
// +----------------------------------------------------------------------
// | Copyright (c)  2025 https://www.qqss.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed QSCMS is not free software, commercial use must purchase official license from official website to avoid unnecessary legal disputes.
// +----------------------------------------------------------------------
// | Author: Contract 990504246@qq.com
// +----------------------------------------------------------------------

namespace app\adminapi\controller\setting;

use app\adminapi\controller\Base;
use app\common\model\Channel as ChannelModel;
use app\common\model\ChannelAccount as ChannelAccountModel;

class CollectionAccount extends Base
{
  /**
   * @notes Payment gateway list
   * @auth true
   */
  public function list()
  {
    $channel_id = inputs('id/d', 0);
    $res        = ChannelAccountModel::where(['channel_id' => $channel_id])->paginate($this->limit)->each(function ($item, $key) {
      $item->code = $item->channel->code;
    });

    $this->success('', [
      'list'  => $res->items(),
      'total' => $res->total(),
    ]);
  }

  /**
   * @notes Get gateway account fields
   * @auth false
   */
  public function getFields()
  {
    $channel_id = inputs('channel_id/d', 0);
    $data       = [];
    $res        = ChannelModel::where([
      'id' => $channel_id,
    ])->find();
    if ($res) {
      if ($res->account_fields != '') {
        $account_fields = explode('|', $res->account_fields);
        foreach ($account_fields as $key => $value) {
          [$label, $name] = explode(':', $value);
          if (isset($name)) {
            $data[$key]['label'] = $label;
            $data[$key]['name']  = $name;
          }
        }
      }
    }

    $this->success('', $data);
  }

  /**
   * @notes Get payment gateway account details
   * @auth false
   */
  public function detail()
  {
    $id   = inputs('id/d', 0);
    $data = ChannelAccountModel::where(['id' => $id])->find();

    return $this->success('', $data);
  }

  private function post()
  {
    $data     = [
      'id'         => inputs('id/d', ''),
      'name'       => inputs('name/s', ''),
      'status'     => inputs('status/d', 0),
      'params'     => inputs('params/a', []),
      'channel_id' => inputs('channel_id/d', 0),
    ];
    $validate = new \app\adminapi\validate\setting\CollectionAccountValidate;
    $validate->failException(true)->check($data);

    return $data;
  }

  /**
   * @notes Add gateway account
   * @auth true
   */
  public function add()
  {
    $data = array_diff_key($this->post(), ['id']);
    $res  = ChannelAccountModel::create($data);

    return $res ? $this->success() : $this->error();
  }

  /**
   * @notes Edit gateway account
   * @auth true
   */
  public function edit()
  {
    $data = $this->post();
    $res  = ChannelAccountModel::update($data);

    return $res ? $this->success() : $this->error();
  }

  /**
   * @notes Delete gateway account
   * @auth true
   */
  public function del()
  {
    $id      = inputs('id/d', 0);
    $account = ChannelAccountModel::where([
      'id' => $id,
    ])->find();
    if (empty($account)) {
      $this->error();
    }
    $res = ChannelAccountModel::destroy($id);

    return $res ? $this->success() : $this->error();
  }

  /**
   * @notes Gateway account status
   * @auth true
   */
  public function status()
  {
    $account_id = inputs('id/d', 0);
    $account    = ChannelAccountModel::where([
      'id' => $account_id,
    ])->find();

    empty($account) && $this->error();

    $status          = inputs('status/d', 1);
    $account->status = $status;
    $res             = $account->save();

    return $res ? $this->success() : $this->error();
  }
}
